import navToNext from '../../../wxjs/navToNext.js'
import Package from '../../../utils/Package.js'
const app = getApp();
console.log(Package)
var data = {
  statusBarHeight: app.globalData.statusBarHeight,
  xhx_images: app.xhx_images,
  xhx_route: app.xhx_route,
  tV2CustomMatchScheduleId: '',

  limitNumber:'3',
  selfChallengeId:null,
  rewardMold: "",

  pageShow:true,
  url: "https://xpwi.github.io/shike/",
};

var currentPage = {
  data,
  onLoad,
  getNumber,
  formSubmit,
  selfAdd,
  copyBtn,

  signUp,
  payment,
};

var page = Object.assign({}, navToNext, currentPage);

Page(page);

function onLoad(options) {
  const that = this;
  const pages = getCurrentPages();
  const prevPage = pages[pages.length - 2]
  if (prevPage.data.rewardMold == 4) {
    this.setData({
      limitNumber: 1,
      rewardMold: 4
    })
  }
}

function getNumber(e){
  var limitNumber = e.detail.value;
  if (limitNumber<3){
    limitNumber=3;
  }
  this.setData({
    limitNumber: limitNumber
  })
}
function formSubmit(e) {
  const that=this;
  var form = e.detail.value;
  console.log('form发生了submit事件，携带数据为：', e.detail.value);
  if (!form.initAmount){
    form.initAmount = '0';
  }
  if (form.limitNumber < 3) form.limitNumber=3;
  if (!form.name){
    wx.showToast({
      title: '挑战赛名称不为空',
      icon: 'none',
      duration: 3000
    });
    return;
  }
  var param = app.createChallegeParam;
  param.initAmount = form.initAmount;
  param.limitNumber = form.limitNumber;
  param.name = form.name;
  that.selfAdd(param);
}
function selfAdd(param){
  const that=this;
  app.middleware.requestAjax({
    serAddr: app.serAddrs.selfAdd,
    data: param,
    cancelToast: true,
    success: function (res) {
      if(res.data){
        that.setData({
          selfChallengeId:res.data.id
        })
        // 无优惠券 version1.0
        // that.signUp(res.data.id)
        // 有优惠券 version2.0
        res.data.price = res.data.price + res.data.initAmount
        wx.navigateTo({
          url: '/pages/pay/pay?isShowCoupon=0&MatchType=2&item=' + JSON.stringify(res.data)
        })
      }
    },
    fail: function (res) {
      app.middleware.showToast(res && res.msg || "加载失败");
    }
  })
}

// 支付
function signUp(id) {
  const that = this;
  var payParam = {};
  payParam.tV2CustomMatchScheduleId = id;
  payParam.wxMold = 1;

  this.setData({
    tV2CustomMatchScheduleId: id
  })
  app.middleware.requestAjax({
    serAddr: app.serAddrs.selfOrderByWXPay,
    data: payParam,
    cancelToast: true,
    header: {
      'content-type': 'application/x-www-form-urlencoded',
    },
    success: function (ores) {
      that.payment(ores.data);
    },
    fail: function (res) {
      app.middleware.showToast(res && res.msg || "请求失败");
    }
  })
}
function payment(data) {
  const that = this;
  wx.requestPayment({
    timeStamp: data.timeStamp,
    nonceStr: data.nonceStr,
    package: data.prepayId,
    signType: data.signType,
    paySign: data.paySign,
    success(res) {
      that.setData({
        pageShow:false
      })
    },
    fail(res) {
      app.middleware.showToast("支付失败");
    },
    complete: function (res) {

    }
  })
}

function copyBtn(e) {
  var that = this;
  // wx.setClipboardData({
  //   //去找上面的数据
  //   data: that.data.url,
  //   success: function (res) {
  //     wx.showToast({
  //       title: '复制成功',
  //     });
  //   }
  // });
  var that = this;
  var url = Package.getPackage().domain + "happyfitLectureshopService/matchScheduleH5/authorize";
  var challengeId = that.data.tV2CustomMatchScheduleId;
  wx.setClipboardData({
    //去找上面的数据
    data: url + '?id=' + challengeId + '&form=1',
    success: function (res) {
      wx.showToast({
        title: '复制成功',
      });
    }
  });
}